//
//  DetailedViewController.m
//  图片故事
//
//  Created by 刘鉴古 on 16/3/9.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "DetailedViewController.h"
#import "Header.h"

@interface DetailedViewController ()
@property (nonatomic ,strong)UIImageView *detailedImage;
@property (nonatomic ,strong)UIButton *saveBtn;
@end
#define Screen self.view.bounds.size
@implementation DetailedViewController

-(UIButton *)saveBtn{
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _saveBtn.frame = CGRectMake(Screen.width - My_Width(50), Screen.height - My_Height(50), My_Width(25), My_Height(25));
        [_saveBtn setImage:[UIImage imageNamed:@"Download"] forState:UIControlStateNormal];
        [_saveBtn addTarget:self action:@selector(saveImageCilck) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}
-(void)saveImageCilck{
    UIImageWriteToSavedPhotosAlbum(self.detailedImage.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message;
    if (!error) {
        message = @"成功保存到相册";
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"图片保存成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [alter show];
    }else
    {
        message = [error description];
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"图片保存失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [alter show];
    }
    NSLog(@"message is %@",message);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    
    _detailedImage = [[UIImageView alloc] init];
    
    [_detailedImage setBounds:CGRectMake(0, 0, SCREEN_W, _bigImage.size.height * (SCREEN_W / _bigImage.size.height))];
    
    [_detailedImage setCenter:self.view.center];
    
    [_detailedImage setUserInteractionEnabled:YES];
    
    //创建捏合手势
    UIPinchGestureRecognizer *pinchGestrue = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(gestEvent:)];
    
    [_detailedImage addGestureRecognizer:pinchGestrue];
    
    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestEvent:)];
    
    [doubleTapGesture setNumberOfTapsRequired:2];
    
    [self.view addGestureRecognizer:doubleTapGesture];
    
    [self.view addSubview:_detailedImage];
    
    [self.detailedImage setImage:_bigImage];
    
    [self.view addSubview:self.saveBtn];
}

#pragma mark --手势响应方法
- (void) gestEvent : (UIGestureRecognizer *)sender{
    
    if([sender isKindOfClass:[UIPinchGestureRecognizer class]]){
        //强转手势
        UIPinchGestureRecognizer *pinchGesture = (UIPinchGestureRecognizer *) sender;
        if (pinchGesture.state == UIGestureRecognizerStateChanged) {
            
            //缩放图片(改变视图的形变属性transfrom)
            //参数为缩放比例 0 - 1
            //CGAffineTransformMakeScale只能缩放一次
            _detailedImage.transform = CGAffineTransformScale(_detailedImage.transform, pinchGesture.scale, pinchGesture.scale);
            
            //初始化缩放比例
            pinchGesture.scale = 1;
        }
    }else {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}


@end
